package 品牌管理;

import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;

public class 查询全部品牌 {
    public static void main(String[] args) {
//        创建list存放品牌
//        泛型
        ArrayList<Brand> brandList = new ArrayList<>();
        Connection conn = null;
        Statement stmt= null;
        ResultSet rs = null;
        try {
            //        1.加载驱动com.mysql.jdbc.Driver
            Class.forName("com.mysql.jdbc.Driver");
//        2.使用驱动管理器（DriverManager）链接数据库
            String url="jdbc:mysql://localhost:3306/db_jdbc_demo20"+
                    "?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";

            String user="root";
            String pass="006688";
            conn = DriverManager.getConnection(url, user, pass);
//        3.书写sql语句
            String sql="SELECT * FROM t_brand ";
//        4.创建执行sql的对象
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
//        5.从rs中取出结果
            while (rs.next()){
                int id = rs.getInt("id");
                String brandName = rs.getString("brand_name");
                String companyName = rs.getString("company_name");
                int ordered = rs.getInt("ordered");
                String description = rs.getString("description");
                int status = rs.getInt("status");
//                System.out.println(id+brandName+companyName+ordered+description+status);
//                封装
                 Brand b = new Brand(id, brandName,
                        companyName, ordered,
                        description, status);
//                brandList.add(b);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                //        6.关闭链接
                rs.close();
                stmt.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
//        访问list
        for (Brand brand : brandList) {
            System.out.println(brand);
        }
    }
}
